<template>
  <el-card>
    <h2>预约服务</h2>
    <el-form :model="form" label-width="100px">
      <el-form-item label="宠物名称">
        <el-select v-model="form.petId" placeholder="请选择自己的宠物，如果没有，请前往宠物信息处添加">
          <el-option
            v-for="pet in pets"
            :key="pet.id"
            :label="pet.name"
            :value="pet.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="服务类型">
        <el-select v-model="form.serviceType" placeholder="请选择服务">
          <el-option label="修毛" value="修毛" />
          <el-option label="美容" value="美容" />
          <el-option label="寄养" value="寄养" />
          <el-option label="洗澡" value="洗澡" />
          <el-option label="治病" value="治病" />
        </el-select>
      </el-form-item>

      <el-form-item label="服务时间">
        <el-date-picker v-model="form.appointmentTime" type="datetime" placeholder="选择时间" />
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" placeholder="填写备注信息" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">提交预约</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores/userStore'
import { storeToRefs } from 'pinia'


const userStore = useUserStore()
const { id, name, token } = storeToRefs(userStore)

const form = ref({
  petId: '',
  serviceType: '',
  appointmentTime: '',
  remark: ''
})

const pets = ref([])

const fetchPets = async () => {
  const res = await axios.get(`/api/server/mine?userId=${id.value}`)
  pets.value = res.data.data
}



onMounted(() => {
  fetchPets()
})

const submitForm = async () => {
  const res = await axios.post('/api/appointment/create', form.value)
  if (res.data.success) {
    ElMessage.success('预约成功')
  } else {
    ElMessage.error('预约失败')
  }
}
</script>
